import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;

public class HttpRequest implements Request {

	private Socket socket;

	public HttpRequest(Socket socket) {
		this.socket = socket;
	}

	Map<String, String> map = null;

	public Map<String, String> getHeaders() {
		if (map == null) {
			getFirst();
		}
		return map;
		
	}

	public String getHeader(String name) {
		if (first == null) {
			getFirst();
		}
		return getHeaders().get("name");
	}

	public String getMethod() {
		if (first == null) {
			getFirst();
		}
		return first.substring(0, first.indexOf(" "));
	}

	Map<String, String> paratemer = null;

	public String getQueryString() {
		paratemer = new HashMap<String, String>();
		String requestURL = getRequestURL();
		int indexOf = requestURL.indexOf("?");
		if (indexOf != -1) {
			String queryString = requestURL.substring(indexOf+1);
			if (queryString.contains("&")) {
				String[] split = queryString.split("&");
				for (String string : split) {
					int indexOf2 = string.indexOf("=");
					String question = string.substring(0, indexOf2);
					String answer = string.substring(indexOf2 + 1);
					paratemer.put(question, answer);
				}
			} else {
				int indexOf2 = queryString.indexOf("=");
				String question = queryString.substring(0, indexOf2);
				String answer = queryString.substring(indexOf2 + 1);
				paratemer.put(question, answer);
			}
			return queryString;
		}
		return null;

	}

	public String getRequestURL() {
		if (first == null) {
			getFirst();
		}
		return first.substring(first.indexOf(" ") + 1, first.indexOf(" HTTP"));
	}

	public String getParameter(String name) {
		if (paratemer == null) {
			getQueryString();
		}
		return paratemer.get(name);
	}

	public String getRemoteAddress() {
		return socket.getInetAddress().getHostAddress();
	}

	public String getRemotePort() {
		return String.valueOf(socket.getPort());
	}

	public InputStream getInputStream() {
		try {
			InputStream inputStream = socket.getInputStream();
			return inputStream;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getUrl() {
		String requestURL = getRequestURL();
		int indexOf = requestURL.indexOf("?");
		if (indexOf != -1) {
			int lastIndexOf = requestURL.lastIndexOf("/");
			if (lastIndexOf != -1) {
				String url = requestURL.substring(lastIndexOf, indexOf);
				return url;
			}
			return "/";
		} else {
			int lastIndexOf = requestURL.lastIndexOf("/");
			if (lastIndexOf != -1) {
				String url = requestURL.substring(lastIndexOf);
				return url;
			}
			return "/";
		}
	}

	public String getContenxtPath() {
		return null;
	}

	String first = null;

	public void getFirst() {
		InputStream inputStream;
		BufferedReader reader = null;
		try {
			map = new HashMap<String, String>();
			inputStream = socket.getInputStream();
			reader = new BufferedReader(new InputStreamReader(inputStream));
			String readLine = reader.readLine();
			first = readLine;
			while ((readLine = reader.readLine())!=null&&(!readLine.equals(""))) {
				int indexOf = readLine.indexOf(":");
				if (indexOf != -1) {
					String key = readLine.substring(0, indexOf);
					String value = readLine.substring(indexOf + 1);
					map.put(key, value);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			
		}

	}

}
